<?
require_once('./application/classes/item/foto.class.php');
class fotoModel extends db implements db_interface{

private static $instance;
private $fotos = array();

public function __construct($iDb){
	
		parent::__construct($iDb);
	
	}
public static function getInstance($iDb){
	
		if(self::$instance == NULL){
			self::$instance = new self($iDb);
		}
		
		return self::$instance;
	
	
	}
	
	
	
	public function getFotoId(item_foto $foto)
	{
		$query = $this -> db -> prepare('SELECT i.item_id,b.bew_titel,b.bew_id,i.item_title,f.foto_path,f.foto_album_id,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y"),(SELECT item_title FROM item WHERE item_id = f.foto_album_id) as album_naam FROM item i join bewonersgroep b on (i.item_geb_id = b.bew_id) join foto f on (i.item_id = f.foto_id) WHERE i.item_id = ? AND i.item_deleted = 1');
		
		$bindValue = $foto -> id;
		
		$query -> bind_param('i', $bindValue);
		$query -> execute();
		
		$query -> bind_result($id,$geb_naam,$geb_id,$title,$path,$album_id,$date,$album_name); 
		
		$query -> fetch();
		
			
			$foto -> foto_album_id = $album_id;
			$foto -> geb_id = $geb_id;
			$foto -> title = $title;
			$foto -> geb_naam = $geb_naam;
			$foto -> foto_path = $path;
			$foto -> geb_id = $foto -> id;
			$foto -> datum = $date;
			$foto -> foto_album_naam = $album_name;
			
			
			
	
	}

	public function getFotos($iUser){
	
		$query = $this -> db -> prepare('SELECT i.item_id,g.geb_naam,i.item_title,f.foto_path,f.foto_album_id,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y"),(SELECT item_title FROM item WHERE item_id = f.foto_album_id) as album_naam FROM item i join gebruikers g on (i.item_geb_id = g.geb_id) join foto f on (i.item_id = f.foto_id) WHERE geb_id = ? AND i.item_deleted = 1');
		
		$bindValue = $iUser -> id;
		
		$query -> bind_param('i', $bindValue);
		$query -> execute();
		
		$query -> bind_result($id,$geb_naam,$title,$path,$album_id,$date,$album); 
		
		while ($query -> fetch())
		{
			$fotos = new item_foto($this -> db);
			$fotos -> id = $id;
			$fotos -> foto_album_naam = $album;
			$fotos -> foto_album_id = $album_id;
			$fotos -> title = $title;
			$fotos -> geb_naam = $geb_naam;
			$fotos -> foto_path = $path;
			$fotos -> geb_id = $iUser -> id;
			$fotos -> datum = $date;
			
			$this -> fotos[] = $fotos;
		}
		
return $this -> fotos;
	
	
	}

	public function add(item_foto $foto){
		
		
		// upload class aanroepen
		$file = new uploadFile($foto -> foto_file);
		
		// randomnaam aanmaken
		$random = new random(20);
		
		
		
		
		$file -> setName($random -> generateNext());
		$file -> setPath('./public/bewonersgroepen/' . $foto -> user -> titel);
		
		while(file_exists($file -> getFullPath())){
			
			$file -> setName($random -> generateNext());
		
		}
		
		$path = '/public/bewonersgroepen/' .$foto -> user -> titel . '/' . $file-> getName();
		
		
		
		$file -> upload();
		$file -> resize(500);
		$thumbName =  $file -> getName(false) . '_thumb';
		
		
		
		
		$file -> copyTo($thumbName, $file -> getPath());
		
		
		
		$file -> resize(220);
		
		
		$query = $this -> db -> prepare("CALL fotoAdd(?,?,?,?)");	

		$bindingvars=array($path,$foto -> title,$foto -> user -> id,$foto -> foto_album_id);
		


		$this -> bindVars($query,$bindingvars);
		
		$query -> execute();
	}
	
	public function delFoto(item_foto $foto)
	{
		$query = $this -> db -> prepare('call itemDel(?);');
		$bindValue = $foto-> id;
		$query -> bind_param('i', $bindValue);
		$query -> execute();
	}
	
	public function uplFoto(item_foto $foto)
	{
		$query = $this -> db -> prepare('call fotoUpd(?,?,?);');
		$bindingVars = array($foto -> title,$foto -> id,$foto -> foto_album_id);
		$this -> bindVars($query,$bindingVars);
		$query -> execute();
	}
	public function fill()
	{
		$query = $this -> db -> query('SELECT i.item_id,bg.bew_titel,g.geb_id,i.item_title,f.foto_path,f.foto_album_id,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") as date FROM item i join gebruikers g on (i.item_geb_id = g.geb_id) join bewonersgroep bg on (bg.bew_id = g.geb_id) join foto f on (i.item_id = f.foto_id) where i.item_deleted = 1 order by item_id desc limit 5;');
		
		
		
		
		while ($row = $query->fetch_assoc())
		{
			$fotos = new item_foto($this -> db);
			$fotos -> foto_id = $row['item_id'];
			$fotos -> foto_album_id = $row['foto_album_id'];
			$fotos -> title = $row['item_title'];
			$fotos -> geb_naam = $row['bew_titel'];
			$fotos -> foto_path = $row['foto_path'];
			$fotos -> geb_id = $row['geb_id'];
			$fotos -> datum = $row['date'];
			
			$this -> fotos[] = $fotos;
		}
		return $this -> fotos;
	}
}



?>